package com.szqhyxj.youxiangju.biz.partnerHome;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.entrace.EntranceManager;
import com.szqhyxj.youxiangju.biz.login.LoginActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantFragmentsContainer;
import com.szqhyxj.youxiangju.biz.merchantHome.home.MerchantHomeFragment;
import com.szqhyxj.youxiangju.biz.merchantHome.my.MyFragment;
import com.szqhyxj.youxiangju.biz.merchantHome.plan.PlansFragment;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.ShopFragment;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.PartnerEquipmentFragment;
import com.szqhyxj.youxiangju.biz.partnerHome.home.PartnerHomeFragment;
import com.szqhyxj.youxiangju.biz.partnerHome.my.PartnerMyFragment;
import com.szqhyxj.youxiangju.biz.partnerHome.shop.PartnerShopFragment;
import com.szqhyxj.youxiangju.sys.util.ToastUtils;
import com.yanzhenjie.permission.AndPermission;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by leung on 2018/10/30.
 */

public class PartnerContainerActivity extends BaseActivity implements PartnerFragmentsContract.View {

    @BindColor(R.color.gray)
    int colorNormal;
    @BindColor(R.color.invitationColor)
    int colorSelected;
    @BindView(R.id.containerPartner)
    ViewPager containerPartner;
    @BindView(R.id.home_icon_partner)
    ImageView homeIconPartner;
    @BindView(R.id.home_title_partner)
    TextView homeTitlePartner;
    @BindView(R.id.home_partner)
    LinearLayout homePartner;
    @BindView(R.id.shop_icon_partner)
    ImageView shopIconPartner;
    @BindView(R.id.shop_title_partner)
    TextView shopTitlePartner;
    @BindView(R.id.shop_partner)
    LinearLayout shopPartner;
    @BindView(R.id.device_icon_partner)
    ImageView deviceIconPartner;
    @BindView(R.id.device_title_partner)
    TextView deviceTitlePartner;
    @BindView(R.id.device_partner)
    LinearLayout devicePartner;
    @BindView(R.id.my_icon_partner)
    ImageView myIconPartner;
    @BindView(R.id.my_title_partner)
    TextView myTitlePartner;
    @BindView(R.id.my_partner)
    LinearLayout myPartner;
    @BindView(R.id.navigator)
    LinearLayout navigator;
    private PartnerFragmentsContract.Presenter presenter;
    private List<Fragment> fragments = new ArrayList<>();
    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    public static void start(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity,PartnerContainerActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner_container);
        ButterKnife.bind(this);
        presenter = new PartnerContainerPresenter(this);
        fragments.add(PartnerHomeFragment.newInstance());
        fragments.add(PartnerShopFragment.newInstance());
        fragments.add(PartnerEquipmentFragment.newInstance());
        fragments.add(PartnerMyFragment.newInstance());
        containerPartner.setAdapter(new PartnerFragmentsContainer(getSupportFragmentManager(), fragments));
        containerPartner.setOffscreenPageLimit(4);
        presenter.init();
        EntranceManager.INSTANCE.setToPartner();
        addPermission();
    }
    private void addPermission(){
        AndPermission.with(this)
                .runtime()
                .permission(permissions)
                .onGranted(permissions -> {
                    // Storage permission are allowed.
                })
                .onDenied(permissions -> {
                    // Storage permission are not allowed.
                    ToastUtils.showTost(getApplicationContext(),"沒有权限，请到设置页面授予权限.");
                })
                .start();
    }

    private void reset() {
        homeIconPartner.setImageResource(R.mipmap.shouye_01);
        shopIconPartner.setImageResource(R.mipmap.gouji_01);
        deviceIconPartner.setImageResource(R.mipmap.shebei_n);
        myIconPartner.setImageResource(R.mipmap.wode_01);
        homeTitlePartner.setTextColor(colorNormal);
        shopTitlePartner.setTextColor(colorNormal);
        deviceTitlePartner.setTextColor(colorNormal);
        myTitlePartner.setTextColor(colorNormal);
    }

    @Override
    public void showHome() {
        containerPartner.setCurrentItem(0, false);
        reset();
        homeIconPartner.setImageResource(R.mipmap.shouye);
        homeTitlePartner.setTextColor(colorSelected);
    }

    @Override
    public void showShop() {
        containerPartner.setCurrentItem(1, false);
        reset();
        shopIconPartner.setImageResource(R.mipmap.gouji);
        shopTitlePartner.setTextColor(colorSelected);
    }

    @Override
    public void showPerformance() {
        containerPartner.setCurrentItem(2, false);
        reset();
        deviceIconPartner.setImageResource(R.mipmap.shebei_l);
        deviceTitlePartner.setTextColor(colorSelected);
    }

    @Override
    public void showMy() {
        containerPartner.setCurrentItem(3, false);
        reset();
        myIconPartner.setImageResource(R.mipmap.wode);
        myTitlePartner.setTextColor(colorSelected);
    }

    @Override
    public void navToLogin() {
        LoginActivity.start(this);
    }

    @Override
    public void showAll() {
        fragments.clear();
        fragments.add(PartnerHomeFragment.newInstance());
        fragments.add(PartnerShopFragment.newInstance());
        fragments.add(PartnerEquipmentFragment.newInstance());
        fragments.add(PartnerMyFragment.newInstance());
        containerPartner.setAdapter(new PartnerFragmentsContainer(getSupportFragmentManager(), fragments));
        containerPartner.setOffscreenPageLimit(4);
    }

    @OnClick({R.id.home_partner, R.id.shop_partner, R.id.device_partner, R.id.my_partner})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_partner:
                showHome();
                break;
            case R.id.shop_partner:
                showShop();
                break;
            case R.id.device_partner:
                showPerformance();
                break;
            case R.id.my_partner:
                showMy();
                break;
        }
    }
}
